!(function($){
	"use strict";
	var datatable;
	var Page = function() {};

	Page.prototype.init = function() {
		this.search();
		this.loadTable();
		this.event();
    };
    
    Page.prototype.search = function(){
    	var $form = $('#search-form');
    	$('#search-btn').click(function(){
            datatable.data($.formParamsToObject($form.serialize()));
            datatable.DataTable().ajax.reload();
    	});
    	
    	$('#reset-btn').click(function(){
    		$form.fillout();
    		datatable.removeData();
    		datatable.DataTable().ajax.reload();
    	});
    },
    
    Page.prototype.event = function(){
    	var $this = this;
    	$('#btn-batch-delete').click(function(){
    		var ids = $(datatable).getSelectAllData();
    		if(ids.length == 0){
    			swal({
                    text: "请至少选中一条数据",
                    type: 'warning',
                    showCancelButton: false,
                    confirmButtonColor: '#4fa7f3',
                    confirmButtonText: '确定'
                });
    			return false;
    		} else {
    			swal({
                    text: "确定删除所选数据吗？",
                    type: 'question',
                    showCancelButton: true,
                    confirmButtonColor: '#4fa7f3',
                    cancelButtonColor: '#d57171',
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(function () {
                	$this.deleteAjax($.arrayToSerial(ids, "ids"));
                });
    		}
    		
    		
    	});
    },
    
    Page.prototype.loadTable = function(){
        datatable = $('#table').dataTable({
            rowId: 'id',
            select: true,
            scrollX: true,
            ajax: {
            	url: contextPath + '/admin/sys/config/page'
            },
            columns : [{
            	title : $.datatableSelectAllTitle(),
                data : "id",
                width: 20,
                bSortable : false,
                render: function ( data, type, row, table ) {
                    return $.datatableSelectAllRow(data);
                }
            }, {
            	title : '#',
                data : "#",
                width: 20,
                bSortable : false,
                render: function ( data, type, row, table ) {
                    return table.settings._iDisplayStart + table.row + 1;
                }
            }, {
            	title : '分类',
                data : "type",
                width: 80,
                bSortable : true,
                render: function ( data, type, row, table ) {
                	return $.dataFinal('types', data);
                }
            }, {
            	title : '内容',
                data : "content",
                bSortable : true
            }, {
            	title : '同级排序',
                data : "sort",
                width: 80,
                bSortable : true
            }, {
            	title : '状态',
                data : "enable",
                width: 80,
                bSortable : true,
                render: function ( data, type, row, table ) {
                	return data ? '<span class="badge badge-success">启用</span>' : '<span class="badge badge-danger">禁用</span>'
                }
            }, {
                title : '操作',
                data : "action",
                width: 120,
                bSortable : false,
                render: function ( data, type, row, table ) {
                	var html = [];
                	html.push('<button type="button" data-id="' + row.id +'" class="btn-modify btn btn-info btn-xs waves-effect waves-light"><i class="fa fa-edit"></i> 修改</button> ');
                	if(!row.enable){
                		html.push('<button type="button" data-id="' + row.id +'" class="btn-enable-yes btn btn-success btn-xs waves-effect waves-light"><i class="fa fa-toggle-on"></i> 启用</button> ');
                	} else {
                		html.push('<button type="button" data-id="' + row.id +'" class="btn-enable-no btn btn-danger btn-xs waves-effect waves-light"><i class="fa fa-toggle-off"></i> 禁用</button> ');
                	}
                    return html.join('');
                }
            } ]
        });
        
        $(datatable).on('click', '.btn-modify', function(){
        	var id = $(this).data().id;
        	location.href = contextPath + '/admin/sys/config/modify/' + id;
        });
        
        $(datatable).on('click', '.btn-enable-yes', function(){
        	var id = $(this).data().id;
        	enableAjax(id, true);
        });
        
        $(datatable).on('click', '.btn-enable-no', function(){
        	var id = $(this).data().id;
        	enableAjax(id, false);
        });
        
        function enableAjax(id, enable){
        	$.ajax({
    			type: 'post',
    			url: contextPath + '/admin/sys/config/update/enable',
    			data: {id: id, enable: enable},
    			dataType: 'json',
    			success: function(result){
    				if(result.success){
    					$.Notification.notify('custom','top center', '通知', '修改成功');
    					 datatable.DataTable().ajax.reload();
    				} else {
    					$.Notification.notify('error','top center', '通知', result.message);
    				}
    			}
    		});
        }
    },
    
    Page.prototype.deleteAjax = function(data){
    	$.ajax({
			type: 'post',
			url: contextPath + '/admin/sys/config/delete',
			data: data,
			dataType: 'json',
			success: function(result){
				if(result.success){
					$.Notification.notify('custom','top center', '通知', '删除成功');
					 datatable.DataTable().ajax.reload();
				} else {
					$.Notification.notify('error','top center', '通知', result.message);
				}
			}
		});
    },
    
    
   
    // init
	$.Page = new Page, $.Page.Constructor = Page;
})(jQuery);
$(function(){
	$.Page.init();
});

